package com.lzx.adx.report.support.strategy.v3.media;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.lzs.adx.commmon.utils.JsonUtils;
import com.lzs.adx.commmon.utils.Result;
import com.lzx.adx.report.annnotions.MediaType;
import com.lzx.adx.report.constant.MediaChannelConstant;
import com.lzx.adx.report.dto.*;
import com.lzx.adx.report.entity.MonitorTemplateEntity;
import com.lzx.adx.report.model.huawei.request.HwUploadRequest;
import com.lzx.adx.report.service.MonitorTemplateService;
import com.lzx.adx.report.support.strategy.v2.media.AbstractCallback;
import com.lzx.adx.report.util.HwSecureUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
@MediaType(MediaChannelConstant.HW)
public class HwCallChannel extends AbstractChannel {

    @Autowired
    private MonitorTemplateService templateService;

    public static final String baseUrl="https://ppscrowd-drcn.op.cloud.huawei.com/action-lib-track/hiad/v2/actionupload";

    @Override
    public ResponseEntity callback(ReportDTO reportDTO, OcpxAccountDTO accountDTO, String callType, String ts) {
        boolean base = base(reportDTO, accountDTO, callType, ts);
        if(!base){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, CallTypeMappingDTO> callTypeMap = getCallTypeMap(accountDTO);
        CallTypeMappingDTO callTypeMappingDTO = callTypeMap.get(callType);
        String eventType=callTypeMappingDTO.getMediaReturnType();

        HwUploadRequest hwUploadRequest = new HwUploadRequest();
        hwUploadRequest.setOaid(reportDTO.getOaid());
        hwUploadRequest.setConversion_type(eventType);
        hwUploadRequest.setTimestamp(ts);
        hwUploadRequest.setCallback(reportDTO.getCallback());
        hwUploadRequest.setCampaign_id(reportDTO.getCampaignId());
        hwUploadRequest.setContent_id(reportDTO.getContentId());
        hwUploadRequest.setConversion_time(String.valueOf(Long.valueOf(ts) / 1000));
        hwUploadRequest.setTracking_enabled("1");
        String paramsBody = JsonUtils.toJsonString(hwUploadRequest);

//        MonitorTemplateEntity monitorTemplateEntity = templateService.selectById(accountDTO.getMediaChannel());


        Map<String, String> header = new HashMap<>();
        header.put("Authorization", HwSecureUtil.buildAuthorizationHeader(paramsBody, accountDTO.getMediaKey()));
        log.info("账号id{},请求id{} 给华为上报回传完整的地址：{},请求参数为{}", reportDTO.getDspId(), reportDTO.getBizRequestId(),
                baseUrl,paramsBody);
        HttpRequest body = HttpUtil.createPost(baseUrl).addHeaders(header).body(paramsBody);
        String result = body.execute().body();
        log.info("账号id{},请求id{} 给华为上报回传结果为 {}", reportDTO.getDspId(), reportDTO.getBizRequestId(), result);

        accountDTO.setFinalURL(baseUrl);
        accountDTO.setBody(result);
        return ResponseEntity.ok(new Result());
    }
}
